package com.begye.smartloan.apptest.disclosure.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager.widget.PagerAdapter
import com.begye.smartloan.apptest.R
import com.begye.smartloan.apptest.tool.addTouch
import org.json.JSONArray
import org.json.JSONObject

class DisclosurePagerAdapter(
    var context: Context,
    var dataList: List<JSONObject>,
    private var clickAction: (Int, Int, String) -> Unit
) : PagerAdapter() {

    private lateinit var disclosureListAdapter: DisclosureListAdapter

    private lateinit var pagerItemDisclosureIcon: ImageView
    private lateinit var pagerItemDisclosureTitle: TextView
    private lateinit var pagerItemDisclosureCheckbox: ImageView
    private lateinit var pagerItemDisclosureDidRead: TextView

    private lateinit var pagerItemDisclosureList: RecyclerView

    private lateinit var pagerItemDisclosureNoOrYes: LinearLayout
    private lateinit var pagerItemDisclosureNo: TextView
    private lateinit var pagerItemDisclosureYes: TextView

    private lateinit var pagerItemDisclosureResultNo: LinearLayout
    private lateinit var pagerItemDisclosureResultYes: LinearLayout

    companion object {
        const val CLICK_READ_CHECK_BOX = 0
        const val CLICK_REJECT = 1
        const val CLICK_AGREE = 2
        const val SHOW_READ_DIALOG = 3

        const val CATEGORY_APP = 100
        const val CATEGORY_SMS = 101
        const val CATEGORY_PHONE = 102
        const val CATEGORY_ALL = 103
    }

    @SuppressLint("ClickableViewAccessibility", "NotifyDataSetChanged")
    override fun instantiateItem(container: ViewGroup, position: Int): Any {
        val view: View =
            View.inflate(context, R.layout.pager_item_disclosure, null)

        pagerItemDisclosureIcon = view.findViewById(R.id.pagerItemDisclosureIcon)
        pagerItemDisclosureTitle = view.findViewById(R.id.pagerItemDisclosureTitle)
        pagerItemDisclosureCheckbox = view.findViewById(R.id.pagerItemDisclosureCheckbox)
        pagerItemDisclosureDidRead = view.findViewById(R.id.pagerItemDisclosureDidRead)

        pagerItemDisclosureList = view.findViewById(R.id.pagerItemDisclosureList)
        pagerItemDisclosureList.layoutManager = LinearLayoutManager(context)

        pagerItemDisclosureNoOrYes = view.findViewById(R.id.pagerItemDisclosureNoOrYes)
        pagerItemDisclosureNo = view.findViewById(R.id.pagerItemDisclosureNo)
        pagerItemDisclosureYes = view.findViewById(R.id.pagerItemDisclosureYes)

        pagerItemDisclosureResultNo = view.findViewById(R.id.pagerItemDisclosureResultNo)
        pagerItemDisclosureResultYes = view.findViewById(R.id.pagerItemDisclosureResultYes)

        disclosureListAdapter = DisclosureListAdapter(emptyList())
        pagerItemDisclosureList.adapter = disclosureListAdapter

        val item = dataList[position]
        val type = item.optInt("type")
        val permission = item.optString("element", "")
        val result = item.optString("result", "")
        val title = item.optString("title", "")
        val resId = item.optInt("resId", R.drawable.dres_13)
        val checkbox = item.optBoolean("checkbox", true)

        if (checkbox) {
            pagerItemDisclosureCheckbox.setImageResource(R.drawable.dres_10)
        } else {
            pagerItemDisclosureCheckbox.setImageResource(R.drawable.dres_9)
        }

        val adapterDataList = mutableListOf<JSONObject>()
        val list = item.optJSONArray("list") ?: JSONArray()
        for (index in 0 until list.length()) {
            val obj = list.getJSONObject(index)
            if (obj != null) {
                adapterDataList.add(obj)
            }
        }
        disclosureListAdapter.dataList = adapterDataList
        disclosureListAdapter.notifyDataSetChanged()

        pagerItemDisclosureIcon.setImageResource(resId)
        pagerItemDisclosureTitle.text = title

        when (result) {
            "NO" -> {
                pagerItemDisclosureResultNo.visibility = View.VISIBLE
                pagerItemDisclosureResultYes.visibility = View.GONE
                pagerItemDisclosureNoOrYes.visibility = View.GONE
            }

            "YES" -> {
                pagerItemDisclosureResultNo.visibility = View.GONE
                pagerItemDisclosureResultYes.visibility = View.VISIBLE
                pagerItemDisclosureNoOrYes.visibility = View.GONE
            }

            else -> {
                pagerItemDisclosureResultNo.visibility = View.GONE
                pagerItemDisclosureResultYes.visibility = View.GONE
                pagerItemDisclosureNoOrYes.visibility = View.VISIBLE
            }
        }

        pagerItemDisclosureCheckbox.setOnClickListener {
            item.put("checkbox", !checkbox)
            clickAction(type, CLICK_READ_CHECK_BOX, permission)

            //
            addTouch(type = "disclosure_dialog_read_check_box_click", permission)
        }

        pagerItemDisclosureNo.setOnClickListener {
            clickAction(type, CLICK_REJECT, permission)

            //
            addTouch(type = "disclosure_dialog_reject_click", permission)
        }

        pagerItemDisclosureYes.setOnClickListener {
            if (!checkbox) {
                clickAction(type, SHOW_READ_DIALOG, permission)
            } else {
                clickAction(type, CLICK_AGREE, permission)
            }

            //
            addTouch(type = "disclosure_dialog_agree_click", permission)
        }

        container.addView(view)
        return view
    }

    override fun setPrimaryItem(container: ViewGroup, position: Int, `object`: Any) {
        super.setPrimaryItem(container, position, `object`)
    }

    override fun getItemPosition(`object`: Any): Int {
        return POSITION_NONE
    }

    override fun getCount(): Int {
        return dataList.size
    }

    override fun isViewFromObject(view: View, `object`: Any): Boolean {
        return view === `object`
    }

    override fun destroyItem(container: ViewGroup, position: Int, obj: Any) {
        container.removeView(obj as View)
    }
}